package com.ruoyi.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.EquipmentVariables;
import com.ruoyi.iot.vo.EquipVariablesVO;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.iot.domain.Coldefinetable;
import com.ruoyi.iot.mapper.ColdefinetableMapper;
import com.ruoyi.iot.service.IColdefinetableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.IdUtil;

import java.util.List;

/**
 * 点位Service业务层处理
 * 
 * @author qiancheng
 * @date 2025-07-15
 */
@Service
public class ColdefinetableServiceImpl extends ServiceImpl<ColdefinetableMapper, Coldefinetable> implements IColdefinetableService
{
    @Autowired
    private ColdefinetableMapper coldefinetableMapper;

    /**
     * 查询点位
     * 
     * @param id 点位主键
     * @return 点位
     */
    @Override
    public Coldefinetable selectColdefinetableById(String id)
    {
        return coldefinetableMapper.selectColdefinetableById(id);
    }

    /**
     * 查询点位列表
     * 
     * @param coldefinetable 点位
     * @return 点位
     */
    @Override
    public List<Coldefinetable> selectColdefinetableList(Coldefinetable coldefinetable)
    {
        return coldefinetableMapper.selectColdefinetableList(coldefinetable);
    }

    /**
     * 新增点位
     * 
     * @param coldefinetable 点位
     * @return 结果
     */
    @Override
    public int insertColdefinetable(Coldefinetable coldefinetable)
    {
        return coldefinetableMapper.insert(coldefinetable);
    }

    /**
     * 修改点位
     * 
     * @param coldefinetable 点位
     * @return 结果
     */
    @Override
    public int updateColdefinetable(Coldefinetable coldefinetable)
    {
        return coldefinetableMapper.updateColdefinetable(coldefinetable);
    }

    /**
     * 批量删除点位
     * 
     * @param ids 需要删除的点位主键
     * @return 结果
     */
    @Override
    public int deleteColdefinetableByIds(String[] ids)
    {
        return coldefinetableMapper.deleteColdefinetableByIds(ids);
    }

    /**
     * 删除点位信息
     * 
     * @param id 点位主键
     * @return 结果
     */
    @Override
    public int deleteColdefinetableById(String id)
    {
        return coldefinetableMapper.deleteColdefinetableById(id);
    }

    /**
     * 导入点位数据
     */
    @Override
    public String importColdefinetable(List<Coldefinetable> coldefinetableList, boolean updateSupport )
    {
        if (coldefinetableList == null || coldefinetableList.size() == 0)
        {
            throw new RuntimeException("导入点位数据不能为空！");
        }
        int successCount = 0;
        int failureCount = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Coldefinetable coldefinetable : coldefinetableList)
        {
            try
            {
                // 验证是否存在这个点位
                Coldefinetable u = this.getOne(new QueryWrapper<Coldefinetable>().lambda()
                        .eq(Coldefinetable::getCode, coldefinetable.getCode()));
                //Coldefinetable u = coldefinetableMapper.selectColdefinetableById(coldefinetable.getId());
                if (u == null)
                {
                    this.insertColdefinetable(coldefinetable);
                    successCount++;
                    successMsg.append("<br/>" + successCount + "、点位 " + coldefinetable.getName() + " 导入成功");
                }
                else if (updateSupport)
                {
                    this.updateColdefinetable(coldefinetable);
                    successCount++;
                    successMsg.append("<br/>" + successCount + "、点位 " + coldefinetable.getName() + " 更新成功");
                }
                else
                {
                    failureCount++;
                    failureMsg.append("<br/>" + failureCount + "、点位 " + coldefinetable.getName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureCount++;
                String msg = "<br/>" + failureCount + "、点位 " + coldefinetable.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureCount > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureCount + " 条数据格式不正确，错误如下：");
            throw new RuntimeException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successCount + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 查询存储设备变量参数信息的列表
     *
     * @param equipmentVariables 存储设备变量参数信息的
     * @return 存储设备变量参数信息的集合
     */
    @Override
    public List<EquipVariablesVO> selectVariablesList(EquipmentVariables equipmentVariables) {
        return coldefinetableMapper.selectVariablesList(equipmentVariables);
    }

    /**
     * 导入点位数据
     * @param coldefinetableList
     * @param updateSupport
     * @return
     */
    @Override
    public String importVariablesVO(List<EquipVariablesVO> coldefinetableList, boolean updateSupport) {
        if (coldefinetableList == null || coldefinetableList.size() == 0)
        {
            throw new RuntimeException("导入点位数据不能为空！");
        }
        int successCount = 0;
        int failureCount = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (EquipVariablesVO coldefinetable : coldefinetableList)
        {
            try
            {
                if (StringUtils.isBlank(coldefinetable.getPlcIp())
                        || StringUtils.isBlank(coldefinetable.getPlcSrc())
                        || StringUtils.isBlank(coldefinetable.getVariableEnName()))
                {
                    continue;
                }
                // 验证是否存在这个点位
                EquipVariablesVO equipVariablesVo = coldefinetableMapper
                        .getEquipVariablesVo(new EquipVariablesVO(coldefinetable.getVariableEnName()));

                if (equipVariablesVo == null)
                {
                    //简单使用
                    long snowflakeId = IdUtil.getSnowflakeNextId();

                    coldefinetableMapper.insertColdefinetableByVo(new Coldefinetable(snowflakeId,coldefinetable));
                    successCount++;
                    successMsg.append("<br/>" + successCount + "、点位 " + coldefinetable.getVariableName() + " 导入成功");
                }
                else if (updateSupport)
                {
                    coldefinetable.setColdId(equipVariablesVo.getColdId());
                    coldefinetableMapper.updateColdefinetableByVo(coldefinetable);
                    successCount++;
                    successMsg.append("<br/>" + successCount + "、点位 " + coldefinetable.getVariableName() + " 更新成功");
                }
                else
                {
                    failureCount++;
                    failureMsg.append("<br/>" + failureCount + "、点位 " + coldefinetable.getVariableName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureCount++;
                String msg = "<br/>" + failureCount + "、点位 " + coldefinetable.getVariableName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureCount > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureCount + " 条数据格式不正确，错误如下：");
            throw new RuntimeException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successCount + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
